package com.qili.util;

import cn.hutool.core.util.ReflectUtil;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.java.Log;

import java.lang.reflect.Field;

/**
 * @author zhaoyongke
 * @date 2022/7/8
 * @description
 */
@Log
public class TrimUtil {
    public static void trimBean(Object model){
        Class clazz = model.getClass();
        Field[] fields = ReflectUtil.getFields(clazz);
        for (Field field : fields) {
            if ("class java.lang.String".equals(field.getGenericType().toString())) {
                Object value = ReflectUtil.getFieldValue(model, field);
                if (value != null) {
                    value = String.valueOf(value).trim();
                    field.setAccessible(true);
                    try {
                        field.set(model, value);
                    } catch (IllegalAccessException e) {
                        log.info("查询条件去除空格异常");
                    }
                }
            }
        }
    }
}
